package server;

import java.io.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;

import additions.*;

//MAIN INTERFACE CLASS
public class ServerInterface extends JFrame {

    protected JButton bExit;
    protected JTextArea taMessages;
    DefaultListModel clientList;
    protected JList lstClients;
    protected JScrollPane scrlClients;
    protected Server server;

    ServerInterface(Server server) {
        super("Instant Messenger Server");

        this.server = server;

        loadInterface();

        setVisible(true);
    }

    protected void loadListeners() {
        bExit.addActionListener(new ExitButton());
        lstClients.addListSelectionListener(new clientSelected());

        addWindowListener(new OnExit());
    }

    protected void loadProperties() {
        setSize(570, 465);
        setResizable(false);

        taMessages.setLineWrap(true);
        taMessages.setEditable(false);

        lstClients.setFixedCellWidth(100);
        lstClients.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        scrlClients = new JScrollPane(lstClients);
        scrlClients.setColumnHeaderView(new JLabel("    Online Users"));

        loadListeners();
    }

    protected void loadInterface() {
        bExit = new JButton("Shut Down");

        taMessages = new JTextArea(20, 40);

        clientList = new DefaultListModel();
        lstClients = new JList(clientList);

        loadProperties();

        Box displayBox = Box.createHorizontalBox();
        displayBox.add(new JScrollPane(taMessages));
        displayBox.add(scrlClients);

        JPanel buttonBox = new JPanel();
        buttonBox.setLayout(new FlowLayout(FlowLayout.CENTER));
        buttonBox.add(bExit);

        Container cp = this.getContentPane();
        cp.setLayout(new BorderLayout());
        cp.add(displayBox, BorderLayout.CENTER);
        cp.add(buttonBox, BorderLayout.SOUTH);

    }

    class OnExit extends WindowAdapter implements WindowListener {

        public void windowClosing(WindowEvent we) {
            server.shutDown();
        }
    }

    class ExitButton implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            server.shutDown();
        }
    }

    class clientSelected implements ListSelectionListener {

        public void valueChanged(ListSelectionEvent e) {
            taMessages.append("User selected: " + lstClients.getSelectedValue().toString() + "\n");
        }
    }


    //CREATES AND DISPLAYS THE CONFIGURE DIALOG
    class ConfigureServerInfo extends JDialog {

        JButton bSave = new JButton("Save");
        JButton bCancel = new JButton("Cancel");
        JLabel lbPort = new JLabel("Port Number : ");
        JLabel lbLimit = new JLabel("Client Limit :    ");
        JTextField tfLimit = new JTextField(6);
        JTextField tfPortNo = new JTextField(6);

        ConfigureServerInfo() {
            super(server.window, "Configure Server", true);

            Box buttonBox = Box.createHorizontalBox();
            buttonBox.add(Box.createHorizontalStrut(15));
            buttonBox.add(bSave);
            buttonBox.add(bCancel);

            Container jcp = this.getContentPane();
            jcp.setLayout(new FlowLayout(FlowLayout.LEFT));
            jcp.add(lbPort);
            jcp.add(tfPortNo);
            jcp.add(lbLimit);
            jcp.add(tfLimit);
            jcp.add(buttonBox);

            bCancel.addActionListener(new CancelButton());

            server.loadSettings();

            tfPortNo.setText(Integer.toString(server.serverPort));

            this.setSize(180, 115);

            Point position = server.window.getLocation();
            position.x = position.x + (server.window.getWidth() / 2) - (this.getWidth() / 2);
            position.y = position.y + (server.window.getHeight() / 2) - (this.getHeight() / 2);
            this.setLocation(position);

            this.setVisible(true);
            this.setResizable(false);
        }

        class CancelButton implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        }
    }
}
